#include<iostream>
#include<string>
#include<unistd.h>
#include<pthread.h>

using namespace std;

//定义与一个条件变量
pthread_cond_t cond;
//定义一个互斥锁
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

void *waitCommand(void*args)
{
    while(true)
    {
        pthread_cond_wait(&cond,&mutex);
        cout << "thread id:" << pthread_self() << "run ..." << endl;

    }
}


int main()
{
    pthread_t t1,t2,t3;
    pthread_create(&t1,nullptr,waitCommand,nullptr);
    pthread_create(&t2,nullptr,waitCommand,nullptr);
    pthread_create(&t3,nullptr,waitCommand,nullptr);
 
    while(true)
    {
        char n = 'a';
        cout << "请输入你的command(n/q): ";
        cin >> n;
        if(n == 'n') 
        {
            pthread_cond_signal(&cond);
        }
        else
        {
             break;
        }

        sleep(1);

    }

    
    pthread_join(t1,nullptr);
    pthread_join(t2,nullptr);
    pthread_join(t3,nullptr);



    return 0;
}








// pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

// int cnt = 100;

// using namespace std;

// void *startRoutine(void *args)
// {


//     string name = static_cast<char*>(args);
//     while(true)
//     {
//         pthread_mutex_lock(&mutex);
//         cout << "count:" << cnt-- << endl;
//         pthread_mutex_unlock(&mutex);

//         sleep(1);
//     }
// }


// int main()
// {
//     pthread_t t1;
//     pthread_t t2;
//     pthread_create(&t1,nullptr,startRoutine,(void*)"thread 1");
//     pthread_create(&t2,nullptr,startRoutine,(void*)"thread 2");
//     pthread_join(t1, nullptr);
//     pthread_join(t2, nullptr);


//     return 0;
// }
